package com.mind.customer.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.mind.common.core.redis.RedisCache;
import com.mind.common.utils.StringUtils;
import com.mind.common.utils.http.HttpUtils;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 接口工具类
 * @author today
 * @date 2025年3月26日 16:34:02
 */
public class InterfaceUtil {
    private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);

    /**
     * 获取token
     * @param redisCache
     * @param userName
     * @param pwd
     * @param countUtils
     * @return
     * @throws IOException
     */
    public static String ncGetToken(RedisCache redisCache,String userName, String pwd, CountUtils countUtils,String url) throws IOException {
        String token = "";
        // 判断是否过期
        // 判断token是否在有效期内
        String ncToken = "";//redisCache.getCacheObject("ncToken");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(ncToken!=null && ncToken.length()>0){
            return ncToken;
        }
        JSONObject params = new JSONObject();
        params.put("username", userName);
        params.put("password", pwd);
        JSONObject body = ncPostRequst(url,"",countUtils,params,"ncGetToken");
        if (body.getInteger("code") == 200) {
            token = body.getJSONObject("data").getString("token");
            redisCache.setCacheObject("ncToken", token, 60 * 60 * 12, TimeUnit.SECONDS);
        }else{
            throw new IOException("获取NcToken失败");
        }
        return token;
    }
    /**
     * Nc-post请求
     * @param url
     * @param token
     * @param countUtils
     * @return
     * @throws IOException
     */
    public static JSONObject ncPostRequst(String url, String token, CountUtils countUtils,JSONObject params,String flag) throws IOException {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();

        OkHttpClient client = builder.readTimeout(20, TimeUnit.MINUTES).build();

        MediaType mediaType = MediaType.parse("application/json;charset=UTF-8");
        RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(params));
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .addHeader("token", token)
                .addHeader("Accept", "*/*")
                .addHeader("Content-Type", "application/json")
                .build();

        Response response = client.newCall(request).execute();
        String bodyStr = response.body().string();
        JSONObject bodyJson = JSONObject.parseObject(bodyStr);
        if (response.code()!=201 ) {
            countUtils.countApi(flag, StringUtils.substring(response.toString(), 0, 1000), 2, url, null, null);
            throw new IOException("Unexpected code " + response);
        }
        countUtils.countApi(flag, StringUtils.substring(bodyStr, 0, 1000), 1, url, null, null);
        return bodyJson;
    }
}
